﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Shrestha.Trigraph
{
    public class Filter
    {

        public static WorkingSet GetFiltered(double minLat, double maxLat, double minLng, double maxLng, WorkingSet maximal)
        {
            WorkingSet subSet = new WorkingSet();
            List<TriEvent> temp = new List<TriEvent>();

            DateTime dateMax = DateTime.MinValue, dateMin = DateTime.MaxValue;
            double latMin = double.MaxValue, lngMin = double.MaxValue, latMax = double.MinValue, lngMax = double.MinValue;

            foreach (TriEvent tg in maximal.TriEventList)
            {
                if ((tg.Latitude >= minLat && tg.Latitude <= maxLat) && (tg.Longitude >= minLng && tg.Longitude <= maxLng))
                {
                    dateMax = dateMax < tg.EventDate ? tg.EventDate : dateMax;
                    dateMin = tg.EventDate < dateMin ? tg.EventDate : dateMin;

                    latMax = latMax < tg.Latitude ? tg.Latitude : latMax;
                    latMin = tg.Latitude < latMin ? tg.Latitude : latMin;

                    lngMax = lngMax < tg.Longitude ? tg.Longitude : lngMax;
                    lngMin = tg.Longitude < lngMin ? tg.Longitude : lngMin;

                    temp.Add(new TriEvent(tg.Latitude, tg.Longitude, tg.EventDate));
                }
            }

            subSet.MaxDbDate = dateMax;
            subSet.MinDbDate = dateMin;

            subSet.MinDbLat = latMin;
            subSet.MaxDbLat = latMax;
            subSet.MinDbLng = lngMin;
            subSet.MaxDbLng = lngMax;

            subSet.TriEventList = temp;
            subSet.TotalRows = subSet.TriEventList.Count;
            subSet.ShowLocationLines = true;

            return subSet;
        }

        public static WorkingSet GetFiltered(DateTime startDate, DateTime endDate, WorkingSet maximal)
        {
            WorkingSet subSet = new WorkingSet();
            List<TriEvent> temp = new List<TriEvent>();

            DateTime dateMax = DateTime.MinValue, dateMin = DateTime.MaxValue;
            double latMin = double.MaxValue, lngMin = double.MaxValue, latMax = double.MinValue, lngMax = double.MinValue;

            foreach (TriEvent tg in maximal.TriEventList)
            {
                if (tg.EventDate >= startDate && tg.EventDate <= endDate)
                {
                    dateMax = dateMax < tg.EventDate ? tg.EventDate : dateMax;
                    dateMin = tg.EventDate < dateMin ? tg.EventDate : dateMin;

                    latMax = latMax < tg.Latitude ? tg.Latitude : latMax;
                    latMin = tg.Latitude < latMin ? tg.Latitude : latMin;

                    lngMax = lngMax < tg.Longitude ? tg.Longitude : lngMax;
                    lngMin = tg.Longitude < lngMin ? tg.Longitude : lngMin;

                    temp.Add(new TriEvent(tg.Latitude, tg.Longitude, tg.EventDate));
                }
            }


            subSet.MaxDbDate = dateMax;
            subSet.MinDbDate = dateMin;

            subSet.MinDbLat = latMin;
            subSet.MaxDbLat = latMax;
            subSet.MinDbLng = lngMin;
            subSet.MaxDbLng = lngMax;

            subSet.TriEventList = temp;
            subSet.TotalRows = subSet.TriEventList.Count;
            subSet.ShowLocationLines = true;

            return subSet;
        }
    }
}
